﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using System.Windows.Input;
using CommunityToolkit.Mvvm.Input;
using EmotionChat.Services;
using LiveChartsCore;
using LiveChartsCore.Measure;
using LiveChartsCore.SkiaSharpView;
using LiveChartsCore.SkiaSharpView.Painting;
using Mvvm.Models;
using Mvvm.Services;
using ReactiveUI;
using SkiaSharp;


namespace Mvvm.ViewModels
{
    
    public partial class PersonalityProfileViewModel : PageViewModelBase
    {
        public ISeries[] _series1;
        public PolarAxis[] AngleAxes1 { get; private set; }
        public string _stringResult1;
        public List<double> _result1{ get; set; }
        public int _flag;

        public ISeries[] _series2;
        public PolarAxis[] AngleAxes2 { get; private set; }
        public string _stringResult2;
        public List<double> _result2 { get; set; }
        public IChatDataProcessedStorage _chatDataProcessedStorage { get; set; }
        public PersonalityProfileService PersonalityProfileService { get; set; }
        
        public LinguisticAnalysisService linguisticAnalysis { get; set; }

        public PersonalityProfileViewModel()
        {
            _chatDataProcessedStorage = new ChatDataProcessedStorage();
            ChangeDataCommand = new AsyncRelayCommand(ChangeUser);
            Initial();
        }
        public string StringResult1 { 
            get => _stringResult1; 
            set => this.RaiseAndSetIfChanged(ref _stringResult1, value); 
        }
        public string StringResult2 { get => _stringResult2; set => this.RaiseAndSetIfChanged(ref _stringResult2, value); }
        public ISeries[] Series1
        {
            get => _series1;
            set => this.RaiseAndSetIfChanged(ref _series1, value);
        }
        public ISeries[] Series2
        {
            get => _series2;
            set => this.RaiseAndSetIfChanged(ref _series2, value);
        }

        public int Flag
        {
            get => _flag;
            set => this.RaiseAndSetIfChanged(ref _flag, value);
        }
        public ICommand ChangeDataCommand { get; set; }
        private async Task InitializeData()
        {
           var chatdata = new ChatDataProcessedStorage();
           await chatdata.InitializeAsync();
           var chatDataProcessed = await chatdata.QueryAsync(1);
           PersonalityProfileService = new PersonalityProfileService(chatDataProcessed[0],Flag);
           linguisticAnalysis = new LinguisticAnalysisService(chatDataProcessed[0],Flag);
           linguisticAnalysis.flag = Flag;
            // 用personalityprofileService生成雷达图
            _result1 = PersonalityProfileService.GenerateReport();
            _result2 = linguisticAnalysis.GenerateLinguisticReport();
            StringResult1 = PersonalityProfileService.stringReport;
            StringResult2 = linguisticAnalysis.stringReport;
        }
        public  async Task ChangeUser()
        {
            Flag = Flag != 1 ? 1 : 0;
            await Initial();
            // 循环打印_result1数组
            Console.WriteLine("切换Flag:"+Flag);
        }
        private async Task Initial()
        {
            await InitializeData();
            Series1 =
            [
                new PolarLineSeries<double>
                {
                    Values = _result1,
                    LineSmoothness = 1,
                    GeometrySize = 0,
                    Name = "语言分析",
                    Fill = new SolidColorPaint(SKColors.Blue.WithAlpha(90),4),
                    Stroke = new SolidColorPaint(SKColors.Blue.WithAlpha(90),4)
                }
            ];
            AngleAxes1 =
            [
                new PolarAxis
                {
                    LabelsRotation = LiveCharts.TangentAngle,
                    Labels = ["社交性", "情绪稳定性", "开放性", "责任感", "神经质"]
                }
            ];
            Series2 =
            [
                new PolarLineSeries<double>
                {
                    Values = _result2,
                    LineSmoothness = 1,
                    GeometrySize = 0,
                    Fill = new SolidColorPaint(SKColors.Red.WithAlpha(90)),
                    Name = "语言分析",
                    Stroke = new SolidColorPaint(SKColors.Red.WithAlpha(90),4)
                }
            ];
            AngleAxes2 = new[]
            {
                new PolarAxis
                {
                    LabelsRotation = LiveCharts.TangentAngle,
                    Labels = [" 简洁性", " 直接性", " 情感表达性", " 互动性"]
                }
            };
        }
        public override bool CanNavigateNext 
        { 
            get => true;
            protected set => throw new NotSupportedException(); 
        }

        // You cannot go back from this page
        public override bool CanNavigatePrevious
        {
            get => true;
            protected set => throw new NotSupportedException();
        }    
    }
}
